package com.zhuzicc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * redis实例controller
 *
 * @author zhaoxiang
 * @date 2022-03-27 21:28:06
 */
@RequestMapping("redis/demo")
@RestController
public class RedisDemoController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 数据还原
     *
     * @return
     */
    @GetMapping("/reset")
    public void reset() {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        redisTemplate.delete("user:1001");
        ops.set("goods:1001", 10);
    }

    /**
     * 秒杀场景模拟一
     *
     * @return
     */
    @GetMapping("/seckill")
    public String seckill() {
        int userId = new Random().nextInt(1000);
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();

        if (Integer.parseInt(Objects.requireNonNull(ops.get("goods:1001")).toString()) > 0) {
            ops.decrement("goods:1001");
            listOperations.leftPush("user:1001", String.valueOf(userId));
            System.out.println(listOperations.range("user:1001", 0, -1));
            return "success";
        } else {
            return "fail";
        }
    }

    /**
     * 秒杀场景模拟二
     *
     * @return
     */
    @GetMapping("/seckill/lua")
    public String seckillWithLua() {
        int userId = new Random().nextInt(1000);

        String script = "if tonumber(redis.call('get', KEYS[1]))>0 then redis.call('decr',KEYS[1]) redis.call('lpush', KEYS[2], ARGV[1]) return 1 else return 0 end";
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(script);
        redisScript.setResultType(Long.class);

        List<String> keysList = new ArrayList<>();
        keysList.add("goods:1001");
        keysList.add("user:1001");

        Long result = (Long) redisTemplate.execute(redisScript, keysList, String.valueOf(userId));

        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();

        if (result == 1) {
            System.out.println(ops.get("goods:1001"));
            System.out.println(listOperations.range("user:1001", 0, -1));
            return "success";
        } else {
            return "fail";
        }
    }
}
